/**
 * Notes: 宠物模块控制器 - 宠物门店预约小程序
 * Date: 2025-01-27 
 */

const BaseController = require('./base_controller.js');
const PetService = require('../service/pet_service.js');
const contentCheck = require('../../framework/validate/content_check.js');
const UserModel = require('../model/user_model.js');
const PassportService = require('../service/passport_service.js');
const config = require('../../config/config.js');

class PetController extends BaseController {

	/**
	 * 获取我的宠物列表
	 */
	async getPetList() {
		let service = new PetService();
		return await service.getPetList(this._userId);
	}

	/**
	 * 获取宠物详情
	 */
	async getPetDetail() {
		// 数据校验
		let rules = {
			petId: 'must|string|min:1|max:50|name=宠物ID',
		};

		let input = this.validateData(rules);

		let service = new PetService();
		return await service.getPetDetail(input.petId, this._userId);
	}

	/**
	 * 添加宠物
	 */
	async addPet() {
		this.ensureAuthenticatedForWrite();
		// 数据校验
		let rules = {
			name: 'must|string|min:1|max:20|name=宠物昵称',
			gender: 'must|string|in:公,母|name=性别',
			category: 'must|string|in:狗狗,猫猫|name=类别',
			weight: 'float|min:0|max:100|name=体重',
			isSterilized: 'bool|name=是否绝育',
			species: 'string|max:50|name=品种',
			avatar: 'string|max:200|name=头像',
			birthday: 'int|min:0|name=生日',
		};

		let input = this.validateData(rules);

		// 内容审核
		await contentCheck.checkTextMultiClient(input);

		// TEST_MODE 下直接返回模拟数据，避免访问云数据库
		if (config.TEST_MODE) {
			return {
				_id: 'pet_test_' + Date.now(),
				userId: this._userId,
				name: input.name,
				gender: input.gender,
				category: input.category,
				weight: input.weight || 0,
				isSterilized: !!input.isSterilized,
				species: input.species || '',
				avatar: input.avatar || '',
				birthday: input.birthday || 0,
				createTime: this._timestamp,
				updateTime: this._timestamp
			};
		}

		// 用户存在性预检与自动建档兜底（非测试模式）
		let user = await UserModel.getOne({ _id: this._userId });
		if (!user) {
			const passport = new PassportService();
			await passport.registerUser(this._userId, {});
		}

		let service = new PetService();
		return await service.addPet(this._userId, input);
	}

	/**
	 * 更新宠物信息
	 */
	async updatePet() {
		this.ensureAuthenticatedForWrite();
		// 数据校验
		let rules = {
			petId: 'must|string|min:1|max:50|name=宠物ID',
			name: 'string|min:1|max:20|name=宠物昵称',
			gender: 'string|in:公,母|name=性别',
			category: 'string|in:狗狗,猫猫|name=类别',
			weight: 'number|min:0|max:100|name=体重',
			isSterilized: 'boolean|name=是否绝育',
			species: 'string|max:50|name=品种',
			avatar: 'string|max:200|name=头像',
			birthday: 'int|min:0|name=生日',
		};

		let input = this.validateData(rules);

		// 内容审核
		await contentCheck.checkTextMultiClient(input);

		// TEST_MODE 下直接返回模拟数据，避免访问云数据库
		if (config.TEST_MODE) {
			return {
				_id: input.petId,
				userId: this._userId,
				name: input.name,
				gender: input.gender,
				category: input.category,
				weight: input.weight,
				isSterilized: input.isSterilized,
				species: input.species,
				avatar: input.avatar,
				birthday: input.birthday,
				updateTime: this._timestamp
			};
		}

		// 用户存在性预检与自动建档兜底（非测试模式）
		let user = await UserModel.getOne({ _id: this._userId });
		if (!user) {
			const passport = new PassportService();
			await passport.registerUser(this._userId, {});
		}

		let service = new PetService();
		return await service.updatePet(input.petId, this._userId, input);
	}

	/**
	 * 删除宠物
	 */
	async deletePet() {
		this.ensureAuthenticatedForWrite();
		// 数据校验
		let rules = {
			petId: 'must|string|min:1|max:50|name=宠物ID',
		};

		let input = this.validateData(rules);

		// TEST_MODE 下直接返回模拟数据，避免访问云数据库
		if (config.TEST_MODE) {
			return true;
		}

		// 用户存在性预检与自动建档兜底（非测试模式）
		let user = await UserModel.getOne({ _id: this._userId });
		if (!user) {
			const passport = new PassportService();
			await passport.registerUser(this._userId, {});
		}

		let service = new PetService();
		return await service.deletePet(input.petId, this._userId);
	}

	/**
	 * 获取宠物统计信息
	 */
	async getPetStats() {
		let service = new PetService();
		return await service.getPetStats(this._userId);
	}
}

module.exports = PetController;
